<?php
namespace jy_ylfee;
defined('BY_JYA') or exit('error');
/*
	区域代理分红计算
	
*/
class commission extends \plugin{
	/*
		生成佣金
	*/
	function create($order,$nobuy=0){
		$isopen=intval($this->setting['isopen']);
		if(!$isopen){
			return;
		}
		if($order['mid']){
			$this->model('member')->register();
		}
		$this->order=$order;
        //获取地址
        $order['address']=$order['address']?json_decode($order['address'],true):array();
        if($order['address'] && ($order['address']['name'] || $order['address']['adddress'])){
            $address=$order['address'];
        }elseif($order['addressid']){
            $address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']),'pcd');
        }
		$address=$address['pcd']?:$order['pcd'];
        if(!is_array($address)){
            $address=json_decode($address,true);
        }
		if($address){
			$areapcd=$this->model('model')->getarea($address);
		}
		if($order['redpcd']){
			$areapcd=$order['redpcd'];
		}
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$this->order['mid'],
			'ordersn'=>$this->order['ordersn'],
			'order_price'=>$this->order['price'],
			'commission_money'=>0,
			'province'=>$areapcd['province'],
			'city'=>$areapcd['city'],
			'createtime'=>TIMESTAMP,
		);
		//佣金计算方式，是否扣除运费
		if($this->setting['commissiontype']==1){
			$this->commission_order['commission_price']=$this->order['price'];
		}else{
			$this->commission_order['commission_price']=$this->order['price']-$this->order['dispatchprice'];
		}
		//直荐分润,间荐分润
		$fromuser=pdo_getcolumn('core_members',array('id'=>$this->order['mid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		for($i=0;$i<2;$i++){
			if(!$fromuser){
				break;
			}
			$member=$this->get('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
			if($member['status'] && $member['is_agent']){
				$this->create_commission($member,$i);
			}
			if($member['level_id'] && !$i){
				break;
			}
			$fromuser=pdo_getcolumn('core_members',array('id'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'fromuser');
		}
		if($areapcd){
			//计算省级代理分红
			$this->province_commission($areapcd);
			$this->city_commission($areapcd);
		}
		//生成分销订单
		if($this->commission_order['isarea']){
			$indata=$this->commission_order;
			unset($indata['isarea']);
			$this->insert('order',$indata);
		}
		return true;
	}
	public function province_commission($areapcd){
		$agents=$this->getall('member',array('mid <>'=>$this->order['mid'],'level_id'=>2,'uniacid'=>$_SESSION['uniacid'],'is_agent'=>1,'province'=>$areapcd['province']));
		if(!$agents){
			return;
		}
		foreach($agents as $agent){
			$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent['mid']));
			if(!$member || $member['stop']){
				continue;
			}
			$this->create_commission($agent,12,true);
		}
	}
	public function city_commission($areapcd){
		if(!$areapcd['city']){
			return;
		}
		$agents=$this->getall('member',array('mid <>'=>$this->order['mid'],'level_id'=>1,'uniacid'=>$_SESSION['uniacid'],'is_agent'=>1,'province'=>$areapcd['province'],'city'=>$areapcd['city']));
		if(!$agents){
			return;
		}
		foreach($agents as $agent){
			$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent['mid']));
			if(!$member || $member['stop']){
				continue;
			}
			$this->create_commission($agent,11,true);
		}
	}
	/*
		给指定区域代理计算分红
	*/
	function create_commission($agent,$i,$isarea=false,$ismerch=false){
		$fenset=$this->setting['fenset'];
		foreach($fenset as $fv){
			if($agent['level_id']==$fv['key']){
				$feeinfo=$fv;
				$feerate=$fv['feerate'];
				$tuifee0=$fv['tuifee0'];
				$tuifee1=$fv['tuifee1'];
				break;
			}
		}
		$order_price=$this->order['price'];
		$fee=0;
		$tuifee0fee=0;
		$tuifee1fee=0;
		$this->needorder=false;
		if(!$this->order['goods']){
			$this->needorder=true;
		}
        $total_lastcostprice=0;
		$yao_goods=$this->setting['yao_goods'];
		$yao_isopen=intval($this->setting['yao_isopen']);
		foreach($this->order['goods'] as $goods){
			if($this->isyaoorder){
				continue;
			}
			if(!$goods['fee_desc'] && $goods['merchid']){
				model('jy_weishop/order')->setfee_desc($goods['orderid']);
			}
			$total_price=$goods['total_price']?:pdo_getcolumn('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['id']),'total_price');
			$costprice=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['goodsid']),'costprice');
			$total_costprice=$goods['total']*$costprice;
			$total_lastcostprice +=$total_costprice;
			$goods_commission=$this->get('goods',array('uniacid'=>$_SESSION['uniacid'],'goods_id'=>$goods['goodsid']));
			if($yao_isopen){
				foreach($yao_goods as $bgv){
					if($bgv['id']==$goods['goodsid']){
						$this->isyaoorder=true;
						$this->needorder=false;
						continue;
					}
				}
			}
			if(!$goods_commission['id']){
				$this->needorder=true;
				continue;
			}
			if($goods_commission['ignore']){
				$order_price=$order_price-$goods['total_price'];
				continue;
			}
			//如果商品单独设置佣金，将减少本商品的金额
			$order_price=$order_price-$total_price;
			$goods_commission['commissions']=$goods_commission['commissions']?json_decode($goods_commission['commissions'],true):array();
			foreach($goods_commission['commissions'] as $gc){
						if($gc['level_id']==0){
							$demogoodsfeerate=$gc['feerate'];
							$demogoodstuifee0=$gc['tuifee0'];
							$demogoodstuifee1=$gc['tuifee1'];
						}
						if($gc['level_id']!=$agent['level_id']){
							continue;
						}else{
							$goodsfeerate=$gc['feerate'];
							$goodstuifee0=$gc['tuifee0'];
							$goodstuifee1=$gc['tuifee1'];
							break;
						}
			}
			$goodsfeerate=$goodsfeerate?:$demogoodsfeerate;
			$goodstuifee0=$goodstuifee0?:$demogoodstuifee0;
			$goodstuifee1=$goodstuifee1?:$demogoodstuifee1;
			//利润计算
			if($this->setting['commissionfeetype']==2){
				$total_price -=$total_costprice;
				$total_lastcostprice -=$total_costprice;
			}
			if(substr($goodsfeerate,-1)=='%'){
				$fee+=($total_price*substr($goodsfeerate,0,-1))/100;
			}else{
				$fee+=$goodsfeerate*$goods['total'];
			}
			if(substr($goodstuifee0,-1)=='%'){
				$tuifee0fee+=($total_price*substr($goodstuifee0,0,-1))/100;
			}else{
				$tuifee0fee+=$goodstuifee0*$goods['total'];
			}
			if(substr($goodstuifee1,-1)=='%'){
				$tuifee1fee+=($total_price*substr($goodstuifee1,0,-1))/100;
			}else{
				$tuifee1fee+=$goodstuifee1*$goods['total'];
			}
		}
		if($this->needorder && $order_price>0){
					if(substr($feerate,-1)=='%'){
						$fee+=($order_price*substr($feerate,0,-1))/100;
					}else{
						$fee+=$feerate*$goods['total'];
					}
					if(substr($tuifee0,-1)=='%'){
						$tuifee0fee+=($order_price*substr($tuifee0,0,-1))/100;
					}else{
						$tuifee0fee+=$tuifee0*$goods['total'];
					}
					if(substr($tuifee1,-1)=='%'){
						$tuifee1fee+=($order_price*substr($tuifee1,0,-1))/100;
					}else{
						$tuifee1fee+=$tuifee1*$goods['total'];
					}
		}
		if($this->isyaoorder){
			$yaofeerate=$i?$this->setting['yao_rate2']:$this->setting['yao_rate'];
			$yaofee=($order_price*$yaofeerate)/100;
			if($yaofee>0 && $i<=1 && !$isarea){
				$this->commission_order['commission_money']+=$yaofee;
				$this->commission_order['type']=2;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'fromuser'=>$agent['id'],
					'area_code'=>$agent['area_code'],
					'level'=>$i?10:9,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$yaofee,
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['is_jiesuan']?:0,
				);
				$this->insert('commission',$new_commission);
			}
		}
		//区域分润
		if($fee>0 && $isarea){
				$this->commission_order['commission_money']+=$fee;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'fromuser'=>$agent['id'],
					'area_code'=>$agent['area_code'],
					'level'=>1,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$fee,
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['is_jiesuan']?:0,
				);
				$this->insert('commission',$new_commission);
		}
		//一级分销
		if($tuifee0fee>0 && !$i && !$isarea){
				$this->commission_order['commission_money']+=$tuifee0fee;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'fromuser'=>$agent['id'],
					'area_code'=>$agent['area_code'],
					'level'=>2,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$tuifee0fee,
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['is_jiesuan']?:0,
				);
				$this->insert('commission',$new_commission);
		}
		//二级分销
		if($tuifee1fee>0 && $i==1 && !$isarea){
				$this->commission_order['commission_money']+=$tuifee1fee;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'fromuser'=>$agent['id'],
					'area_code'=>$agent['area_code'],
					'level'=>3,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$tuifee1fee,
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['is_jiesuan']?:0,
				);
				$this->insert('commission',$new_commission);
		}
		//判断是否存在区域代理
		$this->commission_order['isarea']=true;
		//此处判断分销商是否满足升级条件，如果满足则升级分销商等级
		$agent_update=array(
			'order_price +='=>$this->commission_order['order_price'],
			'order_num +='=>1,
		);
		$this->update('member',$agent_update,array('id'=>$agent['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	/*
		订单完成时，把佣金状态由初始的订单待完成升级为待结算
	*/
	public function order_complete($order){
		$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('status'=>0,'ordersn'=>$order['ordersn']));
	}
	/*
		发工资
	*/
	public function upfee5(){
		$month = date('Y-m',strtotime('-1 month'));
		$params=array('month'=>$month,'type'=>array(4,5),'uniacid'=>$_SESSION['uniacid']);
		$feelog=pdo_get('jy_ylfee_feelog',$params);
		if($feelog){
			exi('上月已经发工资了，不可重复！','error');	
		}
		$params=array('ispay'=>1,'uniacid'=>$_SESSION['uniacid']);
		$yuestart = mktime(0,0,0,date('m'),1,date('Y'));
		$yueend = mktime(0,0,0,date('m')-1,1,date('Y'));
		$params['paytime between']=array($yuestart,$yueend);
		$getfee=pdo_getsum('jy_ylfee_apply',$params,'fee');
		$payfee=0;
		$num=0;
		$params=array('is_agent'=>1,'status'=>1,'uniacid'=>$_SESSION['uniacid']);
		$members=pdo_getall('jy_ylfee_member',$params);
		$areas=$this->model()->available_areas();
		$shengliufee=$this->setting['shengliufee'];
		paixu($shengliufee,'man','desc');
		foreach($members as $mv){
			$onefee=$this->onememberfee(4,$mv,array($yuestart,$yueend),$areas,$shengliufee,$month);
			if($onefee>0){
				$payfee +=$onefee;
				$num++;
			}
			$onefee=$this->onememberfee(5,$mv,array($yuestart,$yueend),$areas,$shengliufee,$month);
			if($onefee>0){
				$payfee +=$onefee;
				$num++;
			}
		}
		$feelog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'month'=>$month,
			'getfee'=>$getfee,
			'type'=>5,
			'payfee'=>$payfee,
			'num'=>$num,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('feelog',$feelog);
		return '';
	}
	/*
		发股份
	*/
	public function upfee6(){
		$month = date('Y-m',strtotime('-1 month'));
		$params=array('month'=>$month,'type'=>6,'uniacid'=>$_SESSION['uniacid']);
		$feelog=pdo_get('jy_ylfee_feelog',$params);
		if($feelog){
			exi('上月已经发股份分红了，不可重复！','error');	
		}
		$params=array('ispay'=>1,'uniacid'=>$_SESSION['uniacid']);
		$yuestart = mktime(0,0,0,date('m'),1,date('Y'));
		$yueend = mktime(0,0,0,date('m')-1,1,date('Y'));
		$params['paytime between']=array($yuestart,$yueend);
		$getfee=pdo_getsum('jy_ylfee_apply',$params,'fee');
		$payfee=0;
		$num=0;
		$areas=$areas?:$this->model()->available_areas();
		$params=array('is_agent'=>1,'status'=>1,'uniacid'=>$_SESSION['uniacid']);
		$members=pdo_getall('jy_ylfee_member',$params);
		foreach($members as $mv){
			$onefee=$this->onememberfee(6,$mv,array($yuestart,$yueend),$areas,$shengliufee,$month);
			if($onefee>0){
				$payfee +=$onefee;
				$num++;
			}
			
		}
		$feelog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'month'=>$month,
			'getfee'=>$getfee,
			'type'=>6,
			'payfee'=>$payfee,
			'num'=>$num,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('feelog',$feelog);
		return '';
	}
	public function onememberfee($type,$member,$paytime=array(),$areas=array(),$shengliufee=array(),$month=''){
		$areas=$areas?:$this->model()->available_areas();
		$isgetinfo=$month?0:1;
		$month=$month?:date('Y-m');
		$level_id=$member['level_id']?:0;
		if(!$level_id){
			return 0;
		}
		if($type==4){
			$gufeerate=$this->setting['fenset'][$level_id]['mtuifee0'];	
		}elseif($type==5){
			$gufeerate=$this->setting['fenset'][$level_id]['mtuifee1'];	
		}elseif($type==6){
			$gufeerate=$this->setting['fenset'][$level_id]['gufee'];	
		}
		$gufeerate=intval($gufeerate);
		if(!$gufeerate){
			return 0;
		}
		$getgufee=0;
		$alljifee=0;
		$params=array('ispay'=>1,'uniacid'=>$_SESSION['uniacid']);
		if($member['mid']){
			$params['mid <>']=$member['mid'];
		}
		$params['paytime between']=$paytime;
		if($type==4 || $type==6){
			if(!$level_id){
				$params['province']=$member['province'];
				$params['city']=$member['city'];
				$params['level_id']=0;
				$getgufee=pdo_getsum('jy_ylfee_apply',$params,'fee');
				if($type==6){
					$params['is_agent']=1;
					$params['status']=1;
					$mnum=pdo_count('jy_ylfee_member',$params);
					$getgufee=$mnum?$getgufee/$mnum:0;
				}
				$alljifee=$getgufee;
			}elseif($level_id==1){
				$params['province']=$member['province'];
				$params['city']=$member['city'];
				$getgufee=pdo_getsum('jy_ylfee_apply',$params,'fee');
				$alljifee=$getgufee;
			}elseif($level_id==2){
				$params['province']=$member['province'];
				if($this->setting['shengcityminfee']>0){
					$citys=$areas[$member['province']];
					foreach($citys as $city){
						$params['city']=$city;
						$cityfee=pdo_getsum('jy_ylfee_apply',$params,'fee');
						$alljifee +=$cityfee;
						if($cityfee>=$this->setting['shengcityminfee']){
							$getgufee +=$cityfee;
						}
					}
				}else{
					$getgufee=pdo_getsum('jy_ylfee_apply',$params,'fee');
					$alljifee=$getgufee;
				}
			}
		}elseif($type==5){
			if($level_id==1 && $this->setting['cityfeerate']>0){
				//旗下的合伙人推荐的其他城市的流水5%
				$downcodes=pdo_getall('jy_ylfee_member',array('area_code <>'=>$member['area_code'],'level_id'=>1,'fromuser'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),'area_code','area_code');
				$hehuomids=pdo_getall('jy_ylfee_member',array('level_id'=>0,'area_code'=>$member['area_code'],'uniacid'=>$_SESSION['uniacid']),'mid','mid');
				$hehuomids=$hehuomids?array_keys($hehuomids):-1;
				$hehuocodes=pdo_getall('jy_ylfee_member',array('area_code <>'=>$member['area_code'],'level_id'=>1,'fromuser'=>$hehuomids,'uniacid'=>$_SESSION['uniacid']),'area_code','area_code');
				$area_codes=array_merge($downcodes,$hehuocodes);
				$area_codes=$area_codes?array_keys($area_codes):-1;
				$params['area_code']=$area_codes;
				$params['level_id']=array(0,1);
				$otherfee=pdo_getsum('jy_ylfee_apply',$params,'fee');
				$getgufee=$otherfee*$this->setting['cityfeerate']/100;
				$alljifee=$getgufee;
			}elseif($level_id==2){
				//省内所有合伙人或者是市公司或者是自己推荐的省级流水（1-3个省给1%，4-9个省给3%，10个省以上给5%）
				$downcodes=pdo_getall('jy_ylfee_member',array('area_code <>'=>$member['area_code'],'level_id'=>2,'fromuser'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),'area_code','area_code');
				$hehuomids=pdo_getall('jy_ylfee_member',array('level_id'=>array(0,1),'area_code'=>$member['area_code'],'uniacid'=>$_SESSION['uniacid']),'mid','mid');
				$hehuomids=$hehuomids?array_keys($hehuomids):-1;
				$hehuocodes=pdo_getall('jy_ylfee_member',array('area_code <>'=>$member['area_code'],'level_id'=>2,'fromuser'=>$hehuomids,'uniacid'=>$_SESSION['uniacid']),'area_code','area_code');
				$area_codes=array_merge($downcodes,$hehuocodes);
				$shengnum=count($area_codes);
				$area_codes=$area_codes?array_keys($area_codes):-1;
				$params['area_code']=$area_codes;
				$params['level_id']=array(0,1,2);
				$otherfee=pdo_getsum('jy_ylfee_apply',$params,'fee');
				$shengliufee=$shengliufee?:$this->setting['shengliufee'];
				paixu($shengliufee,'man','desc');
				foreach($shengliufee as $sv){
					if($shengnum>=$sv['man']){
						$feerate=$sv['rate'];
						break;
					}
				}
				$getgufee=$otherfee*$feerate/100;
				$alljifee=$getgufee;
			}
		}
		$payfee=$getgufee*$gufeerate/100;
		if($payfee>0 && !$isgetinfo){
			$new_commission=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$member['mid'],
				'fromuser'=>$member['id'],
				'area_code'=>$member['area_code'],
				'jifee'=>$getgufee,
				'month'=>$month,
				'level'=>$type,
				'money'=>$payfee,
				'createtime'=>TIMESTAMP,
				'status'=>1,
			);
			$this->insert('commission',$new_commission);
		}
		if($isgetinfo){
			return array('alljifee'=>$alljifee,'jifee'=>$getgufee,'money'=>$payfee);	
		}
		return $payfee;
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$type=0){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		//获取可结算佣金
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1);
		if(!$type){
			$params['level']=array(1,2,3);
		}elseif($type==1){
			$params['level']=array(4,5,6,7,8);
		}elseif($type==2){
			$params['level']=array(9,10);
		}
		if($this->setting['balance_day']){
			//结算天数
			$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
			$params['complete_time <']=$time;
		}
		$commissions=$this->getall('commission',$params);
		if(!$commissions){
			exi('没有可结算的佣金！','error');
		}
		$commission_num=count($commissions);
		$money=0;
		foreach($commissions as $commission){
			$money+=$commission['money'];
		}
		//结算最低佣金检测
		if($money<$this->setting['balance_money']){
			exi('可结算佣金太少，无法结算！','error');
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'commission_num'=>$commission_num,
			'money'=>$money,
			'createtime'=>TIMESTAMP,
			'status'=>0,
			'type'=>$type
		);
		//结算手续费
		if($this->setting['balance_per']){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		foreach($commissions as $commission){
			$update=array(
				'status'=>2,
				'balancesn'=>$new_balance['balancesn'],
			);
			$this->update('commission',$update,array('id'=>$commission['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		$this->model('notice')->notice_fenxiao_tixian($mid,$money);
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn']);
		$update=array(
			'status'=>3,
			'pay_type'=>$pay_type,
			'pay_trade_no'=>$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		$this->update('balance',$update,array('id'=>$balance['id']));
		$this->model('notice')->notice_fenxiao_dakuan($balance['mid'],$balance['final_money']);
		return true;
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,$this->plugin_name,$remark='分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn){
		$params=array(
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'remark'=>'代理商佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		return $res['payment_no'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		exi('暂不支持打款到银行账户！','error');
	}

	public function commission_remark($remark){
		if(!$remark){
			return '';
		}
		$remark=json_decode($remark,true);
		$goods_str='';
		if($remark['goods_commission']){
			foreach($remark['goods_commission'] as $goods){
				$goods['title']=pdo_getcolumn('jy_weishop_goods',array('id'=>$goods['goods_id']),'title');
				$goods_str.="商品ID【{$goods['goods_id']}】，商品名称【{$goods['title']}】，商品数量【{$goods['total']}】，商品总价【{$goods['total_price']}】，佣金规则【{$goods['commission_per']}】，分销佣金【{$goods['commission_money']}】\n";
			}
		}else{
			$goods_str='';
		}
		$remark['level_title']=pdo_getcolumn('jy_ylfee_level',array('uniacid'=>$_SESSION['uniacid'],'level'=>$remark['level_id']),'title');
		$remark['level_title']=$remark['level_title']?:'默认等级';
		$remark_str=<<<TPL
分销商等级ID【{$remark['level_id']}】，等级名称【{$remark['level_title']}】，佣金等级【{$remark['commission_level']}】，订单总价【{$remark['order_price']}】
商品独立佣金：
{$goods_str}
订单佣金：
除去设置独立佣金的商品外订单金额【{$remark['order_commission']['order_price']}】，佣金规则【{$remark['order_commission']['commission_per']}%】，分销佣金【{$remark['order_commission']['commission_money']}】
佣金总计：【{$remark['commission_money']}】
TPL;
		return $remark_str;
	}
}